package com.chen.blue.controller.admin.auth;

import com.chen.blue.domain.model.system.LoginUser;
import com.chen.blue.exception.e.AccountOffException;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.result.R;
import com.chen.blue.security.service.login.LoginService;
import com.chen.blue.security.service.logout.LogoutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created By CLT On 2024/4/27.
 */
@SuppressWarnings("all")
@Api(tags = "认证模块")
@Validated
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private LoginService loginService;

    @Resource
    private LogoutService logOutService;

    @ApiOperation(value = "登录认证", notes = "登录认证")
    @PostMapping("/login")
    public R authByUsernamePassword(@RequestBody @Valid LoginUser loginUser) throws DtoNullException, AccountOffException {

        return R.success("token", loginService.loginHandler(loginUser));

    }

    @ApiOperation(value = "退出登录", notes = "退出登录")
    @PostMapping("/logout")
    public R logoutHandler(HttpServletRequest request, HttpServletResponse response) {

        return R.success(logOutService.logout());

    }

}
